<template>
    <div>
        <el-dialog title="修改用户标签" :visible="true" width="600px" :modal-append-to-body="false"
            :close-on-click-modal="false" @close="handleClose">
            <el-form id="forms" ref="forms" :rules="rulesModel" :model="form" label-width="100px">
                <el-form-item label="用户标签" prop="kdcode">
                    <div class="flex">
                        <el-input v-if="inputVisible" ref="saveTagInput" v-model="inputValue" placeholder="请填写标签内容"
                            class="input-new-tag" size="small" :validate-event="true"
                            @keyup.enter.native="handleInputConfirm" @blur="handleInputConfirm" />

                        <el-button class="button-new-tag" size="small" @click="showInput">+ 添加更多标签</el-button>
                    </div>
                </el-form-item>
                <div>
                    <el-tag v-for="tag in dynamicTags" style="margin-bottom: 10px;" :key="tag" closable
                        :disable-transitions="false" @close="TagClose(tag)">{{ tag }}</el-tag>
                </div>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="handleClose">取 消</el-button>
                <el-button :loading="buttonLoading" type="primary" @click="submit()">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { SetUTag } from "@/api/users.js";
import { isNull } from '@/utils/validate.js'
export default {
    props: {
        info: {
            default: {},
            type: Object
        },
    },
    data() {
        return {
            form: {}, // 详情实体
            rulesModel: {
                power: [
                    { required: true, message: '请输入数量', trigger: 'blur' }
                ],
            },
            buttonLoading: false,

            // 添加标签相关
            dynamicTags: [],
            inputVisible: true,
            inputValue: '',

            coinList: {},// 币种枚举
        }
    },

    mounted() {
        this.setForm();
    },
    methods: {
        /**关闭弹窗 */
        handleClose() {
            this.$parent.modalStatus = 0
        },
        /**表单参数初始化 */
        setForm() {
            this.form = {
                uid: this.info.userid,
                tag: '',
            };
            this.dynamicTags = this.info.tag != '' ? this.info.tag.split(',') : [];// 标签列表
        },
        /////////////////////////////////////////////////// 添加标签 ////////////////////////////////////////////////
        /**删除标签 */
        TagClose(tag) {
            this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1)
        },
        /**输入标签内容 */
        showInput() {
            this.inputVisible = true
            this.$nextTick(_ => {
                this.$refs.saveTagInput.$refs.input.focus()
            })
        },
        /**标签输入监听 */
        handleInputConfirm() {
            const inputValue = this.inputValue
            if (!isNull(inputValue)) {
                this.dynamicTags.push(inputValue)
            }
            this.inputValue = ''
        },
        /**（提交） */
        async submit() {

            this.form.tag = this.dynamicTags.toString();
            if (isNull(this.form.tag)) {
                this.$message({
                    message: '请填写标签',
                    type: "error"
                });
                return;
            }

            try {
                try {
                    await this.$refs['forms'].validate()
                } catch (e) {
                    console.log(e)
                    return
                }
                const { success, msg } = await SetUTag(this.form)
                if (success) {
                    this.handleClose();
                }
                this.$message({
                    message: msg,
                    type: success ? 'success' : 'error'
                })
            } catch (e) {
                // 取消
                console.log(e)
            }
        },
    }
}
</script>

<style lang="scss" scoped>
.grid2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
}

// 标签样式
.el-tag+.el-tag {
    margin-left: 10px;
}

.button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
}

.input-new-tag {
    width: 200px;
    margin-left: 10px;
    vertical-align: bottom;
}
</style>